//
//  GQJCircleView.m
//  TotalDemo
//
//  Created by gujian on 16/6/29.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "GQJCircleView.h"

@interface GQJCircleView()

@property (nonatomic, strong) UIColor *backColor;
@property (nonatomic, strong) UIColor *startColor;
@property (nonatomic, strong) UIColor *endColor;

@property (nonatomic, assign) CGFloat beginRadius;
@property (nonatomic, assign) CGFloat endRadius;

@end

@implementation GQJCircleView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        [self addSubview:subView];
    }
    
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    NSLog(@"%s%d",__func__,__LINE__);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initParams];
    }
    
    return self;
}

- (void)initParams
{
    self.backgroundColor = [UIColor clearColor];
    
    _backColor = UICOLOR_ARGB(0xffd8d8d8);
    
    _startColor = UICOLOR_ARGB(0xfffad963);
    _endColor = UICOLOR_ARGB(0xfff76c1d);
    
    _beginRadius = 30.0;
    _endRadius = 15.0;
}



- (void)drawRect:(CGRect)rect {

    [super drawRect:rect];
    
    if (!rect.size.width || !rect.size.height) {
        return;
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSaveGState(context);
    
    [_backColor setFill];
    CGContextBeginPath(context);
    CGContextAddEllipseInRect(context, rect);
    CGContextClosePath(context);
    
    
    size_t gradLocationsNum = 2;
    CGFloat gradLocations[2] = {0.0, 1.0};
    
    CGFloat componets[8];
    
    for (int i = 0; i < 2; i++) {
        
        CGColorRef colorRef = i>0? _endColor.CGColor : _startColor.CGColor;
        
        size_t nums = CGColorGetNumberOfComponents(colorRef);
        const CGFloat *rgbComponent = CGColorGetComponents(colorRef);
        
        if(nums == 2)
        {
            componets[i*4+0] = rgbComponent[0];
            componets[i*4+1] = rgbComponent[0];
            componets[i*4+2] = rgbComponent[0];
            componets[i*4+3] = rgbComponent[1];
        }else
        {
            componets[i*4+0] = rgbComponent[0];
            componets[i*4+1] = rgbComponent[1];
            componets[i*4+2] = rgbComponent[2];
            componets[i*4+3] = rgbComponent[3];
        }
        
    }
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, componets, gradLocations, gradLocationsNum);
    CGColorSpaceRelease(colorSpace);
    
    CGPoint startCenter = CGPointMake(self.bounds.size.width/2.0, self.bounds.size.height/2.0);
    
    CGContextDrawRadialGradient(context, gradient, startCenter, _beginRadius, startCenter, _endRadius, kCGGradientDrawsAfterEndLocation);
    
    CGGradientRelease(gradient);
    
    
    CGContextRestoreGState(context);
    

}


@end
